package com.axjy.service.impl;

import com.axjy.controller.vo.Node;
import com.axjy.controller.vo.State;
import com.axjy.dao.MenuMapper;
import com.axjy.dao.PermissionMapper;
import com.axjy.dao.UserMapper;
import com.axjy.pojo.Menu;
import com.axjy.pojo.User;
import com.axjy.service.PermissionService;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by asus on 2017/9/20.
 */
@Controller
@Transactional
public class PermissionServiceImpl implements PermissionService{

    @Resource
    private UserMapper userMapper;

    @Resource
    private MenuMapper menuMapper;
    @Resource
    private PermissionMapper permissionMapper;

    @Transactional(readOnly =true)
    public List<Node> search4Tree(Integer rid){
        List<Integer> menuIds=permissionMapper.searchByRole(rid);
        List<Node> list=new ArrayList<>();

        List<Menu> menus=menuMapper.searchLevel01();

        for(Menu m01:menus){
            Node node01=new Node();
            node01.setIcon(m01.getIconSkin());
            node01.setId(m01.getId());
            State state=new State();
            if(menuIds.contains(m01.getId())){
                state.setChecked(true);
            }
            node01.setState(state);
            node01.setText(m01.getName());
            for(Menu m02:m01.getChildren()){
                Node node02=new Node();
                node02.setIcon(m02.getIconSkin());
                node02.setId(m02.getId());
                node02.setNodes(null);
                State state2=new State();
                if(menuIds.contains(m02.getId())){
                    state2.setChecked(true);
                }
                node02.setState(state2);
                node02.setText(m02.getName());
                node01.getNodes().add(node02);
            }
            list.add(node01);
        }
        return list;
    }

    @Override
    public void grant4Role(Integer rid, Integer[] mids) {
        permissionMapper.deleteByRoleId(rid);
        for(Integer mid:mids){
            permissionMapper.addPermission(rid,mid);
        }
    }

    @Override
    public Map<Menu, List<Menu>> searchNav(Integer userId) {
        Map<Menu, List<Menu>> map=new TreeMap<Menu,List<Menu>>();
            User user=userMapper.get(userId);
            Integer roleId=user.getRoleId();
            List<Integer> menuIds=permissionMapper.searchByRole(roleId);
            for(Integer mid01:menuIds){
                Menu menu01=menuMapper.get(mid01);
                if(menu01.getPid()==null){
                    List<Menu> list=new ArrayList<Menu>();
                    map.put(menu01,list);
                    for(Integer mid02:menuIds){
                        Menu menu02=menuMapper.get(mid02);
                        if(menu02.getPid()==mid01){
                            list.add(menu02);
                        }
                    }
                }
            }
           return map;
    }

}
